package lang

import (
	"go_gvm_mini/native"
	"go_gvm_mini/rtda"
)

func init() {
	native.Registers("java/lang/Thread", []*native.NaMethod{
		{
			"currentThread",
			"()Ljava/lang/Thread;",
			currentThread,
		}, {
			"setPriority0",
			"(I)V",
			setPriority0,
		}, {
			"isAlive",
			"()Z",
			isAlive,
		}, {
			"start0",
			"()V",
			start0,
		},
	})
}

// public static native Thread currentThread();
// ()Ljava/lang/Thread;
func currentThread(jFrame *rtda.JFrame) {
	//jThread := jFrame.Thread().JThread()
	classLoader := jFrame.Method().Class().GetClassLoader()
	threadClass := classLoader.LoadClass("java/lang/Thread")
	jThread := threadClass.NewObject()

	threadGroupClass := classLoader.LoadClass("java/lang/ThreadGroup")
	jGroup := threadGroupClass.NewObject()

	jThread.SetRefVar("group", "Ljava/lang/ThreadGroup;", jGroup)
	jThread.SetIntVar("priority", "I", 1)

	jFrame.OperandStack().PushRef(jThread)
}

// private native void setPriority0(int newPriority);
// (I)V
func setPriority0(jFrame *rtda.JFrame) {
	// vars := jFrame.LocalVars()
	// this := vars.GetThis()
	// newPriority := vars.GetInt(1))
	// todo
}

// public final native boolean isAlive();
// ()Z
func isAlive(jFrame *rtda.JFrame) {
	//vars := jFrame.LocalVars()
	//this := vars.GetThis()

	stack := jFrame.OperandStack()
	stack.PushBoolean(false)
}

// private native void start0();
// ()V
func start0(jFrame *rtda.JFrame) {
	// todo
}
